package com.zhuangfei.hos_timetableview.ability;

import com.zhuangfei.hos_timetableview.ResourceTable;
import com.zhuangfei.hos_timetableview.model.MySubject;
import com.zhuangfei.hos_timetableview.model.SubjectRepertory;
import com.zhuangfei.hos_timetableview.utils.ToastUtil;
import com.zhuangfei.timetable.TimetableView;
import com.zhuangfei.timetable.listener.ISchedule;
import com.zhuangfei.timetable.model.Schedule;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;

import java.util.List;

/**
 * 本例子展示如何在Schedule中放入一些额外的信息
 */
public class ExtrasAbility extends Ability {

    private static final String TAG = "ExtrasActivity";

    // 控件
    TimetableView mTimetableView;
    List<MySubject> mySubjects;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_extras);
        // 添加表头的include
        StackLayout stInclude = (StackLayout) findComponentById(ResourceTable.Id_st_include);
        stInclude.addComponent(LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_header_simple, stInclude, false));
        mySubjects = SubjectRepertory.loadDefaultSubjects();
        initTimetableView();
    }

    /**
     * 初始化课程控件
     */
    private void initTimetableView() {
        // 获取控件
        mTimetableView = (TimetableView) findComponentById(ResourceTable.Id_id_timetableView);
        mTimetableView.source(mySubjects)
                .curWeek(1)
                .curTerm("大三下学期")
                .maxSlideItem(10)
                .callback(new ISchedule.OnItemClickListener() {
                    @Override
                    public void onItemClick(Component component, List<Schedule> scheduleList) {
                        display(scheduleList);
                    }
                })
                .showView();
    }

    /**
     * 显示内容
     *
     * @param beans 数据源
     */
    protected void display(List<Schedule> beans) {
        String str = "";
        for (Schedule bean : beans) {
            str += "[" + bean.getName() + "]的id:" + bean.getExtras().get(MySubject.EXTRAS_ID) + "\n";
        }
        ToastUtil.showToast(this, str);
    }
}
